import QtQuick 2.0
import QtQuick.Controls 2.3
import QtQuick.Layouts 1.0
import "storage.js" as Storage

Item {
    id:root
    width: 850
    height: 580
    property real minSize:  Math.min(width/850, height/580)
    property color majorColor: "#44fdfd"

    onFocusChanged:{
        if(!focus) return
        refurbishA()
        refurbishB()
    }

    function readRegSingalLim(handle, sqlName){
        handle.valueText = Storage.readRegSingalLimit(sqlName)
    }
    function writeRegSingalLim(handle, sqlName){
        Storage.writeRegSingalLimit(handle.valueText, sqlName)
    }
    function readRegScope(handle, sqlName){
        handle.tipText = Storage.readRegScope(sqlName)
    }

    function refurbishA()
    {
        readRegSingalLim(text_aNoloadConstantValue, "A套空载长期运行值")
        readRegSingalLim(text_aLoadConstantValue, "A套负载长期运行值")
        readRegSingalLim(text_aOverLimit, "A套过励限制值")
        readRegSingalLim(text_aForceExc, "A套强励限制值")
        readRegSingalLim(text_aHeatRelSpeed, "A套热释放速度")
        readRegSingalLim(text_aForceTime, "A套强励最长时间")
    }
    function refurbishB()
    {
        readRegSingalLim(text_bNoloadConstantValue, "B套空载长期运行值")
        readRegSingalLim(text_bLoadConstantValue, "B套负载长期运行值")
        readRegSingalLim(text_bOverLimit, "B套过励限制值")
        readRegSingalLim(text_bForceExc, "B套强励限制值")
        readRegSingalLim(text_bHeatRelSpeed, "B套热释放速度")
        readRegSingalLim(text_bForceTime, "B套强励最长时间")
    }
    Timer{
        interval: 2000
        repeat: false
        triggeredOnStart: false
        running: true
        onTriggered: {
            readRegScope(text_aNoloadConstantValue, "A套空载长期运行值")
            readRegScope(text_aLoadConstantValue, "A套负载长期运行值")
            readRegScope(text_aOverLimit, "A套过励限制值")
            readRegScope(text_aForceExc, "A套强励限制值")
            readRegScope(text_aHeatRelSpeed, "A套热释放速度")
            readRegScope(text_aForceTime, "A套强励最长时间")
            readRegScope(text_bNoloadConstantValue, "B套空载长期运行值")
            readRegScope(text_bLoadConstantValue, "B套负载长期运行值")
            readRegScope(text_bOverLimit, "B套过励限制值")
            readRegScope(text_bForceExc, "B套强励限制值")
            readRegScope(text_bHeatRelSpeed, "B套热释放速度")
            readRegScope(text_bForceTime, "B套强励最长时间")
        }
    }

    SwipeView
    {
        id: sv_this
        width:parent.width
        height: parent.height*0.85
        clip: true
        Component.onCompleted:{contentItem.highlightMoveDuration = 0}       //将移动时间设为0

        Item{
            Text {
                color: majorColor
                y: 20*root.minSize
                text: qsTr("A通道过励限制参数")
                anchors.horizontalCenter: parent.horizontalCenter
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignHCenter
                font.pixelSize: 40*root.minSize
            }

            Rectangle {
                y: 100*root.minSize
                width: 400*root.minSize
                height: 380*root.minSize
                color: "#6692e8bc"
                radius: 10*root.minSize
                anchors.horizontalCenter: parent.horizontalCenter

                ColumnLayout {
                    property real textSize: 30*root.minSize
                    x: 10*root.minSize
                    width: 380*root.minSize
                    anchors.bottom: parent.bottom
                    anchors.top: parent.top

                    EXC_Normal_DoubleTextSytle3{
                        id: text_aNoloadConstantValue
                        width: parent.width
                        Layout.preferredHeight: 40*root.minSize
                        Layout.fillWidth: true
                        funcText: qsTr("空载长期运行值:")
                        valueText: "???"
                        textSize:parent.textSize
                    }
                    EXC_Normal_DoubleTextSytle3{
                        id: text_aLoadConstantValue
                        width: parent.width
                        Layout.preferredHeight: 40*root.minSize
                        Layout.fillWidth: true
                        funcText: qsTr("负载长期运行值:")
                        valueText: "???"
                        textSize:parent.textSize
                    }
                    EXC_Normal_DoubleTextSytle3{
                        id: text_aOverLimit
                        width: parent.width
                        Layout.preferredHeight: 40*root.minSize
                        Layout.fillWidth: true
                        funcText: qsTr("过励限制值:")
                        valueText: "???"
                        textSize:parent.textSize
                    }
                    EXC_Normal_DoubleTextSytle3{
                        id: text_aForceExc
                        width: parent.width
                        Layout.preferredHeight: 40*root.minSize
                        Layout.fillWidth: true
                        funcText: qsTr("强励限制值:")
                        valueText: "???"
                        textSize:parent.textSize
                    }
                    EXC_Normal_DoubleTextSytle3{
                        id: text_aHeatRelSpeed
                        width: parent.width
                        Layout.preferredHeight: 40*root.minSize
                        Layout.fillWidth: true
                        funcText: qsTr("热释放速度:")
                        valueText: "???"
                        textSize:parent.textSize
                    }
                    EXC_Normal_DoubleTextSytle3{
                        id: text_aForceTime
                        width: parent.width
                        Layout.preferredHeight: 40*root.minSize
                        Layout.fillWidth: true
                        funcText: qsTr("强励最长时间:")
                        valueText: "???"
                        textSize:parent.textSize
                    }
                }
            }

            EXC_Normal_BtnUncheckSecret_highStyle {
                id: btn_ASave
                nameFontpixelSize: 25*root.minSize
                y: 410*root.minSize
                width: 140*root.minSize
                height: 50*root.minSize
                anchors.rightMargin: 10
                anchors.right: parent.right
                nameText: qsTr("保存参数")
                onClicked: {
                    writeRegSingalLim(text_aNoloadConstantValue, "A套空载长期运行值")
                    writeRegSingalLim(text_aLoadConstantValue, "A套负载长期运行值")
                    writeRegSingalLim(text_aOverLimit, "A套过励限制值")
                    writeRegSingalLim(text_aForceExc, "A套强励限制值")
                    writeRegSingalLim(text_aHeatRelSpeed, "A套热释放速度")
                    writeRegSingalLim(text_aForceTime, "A套强励最长时间")
                    timerA_ass.start()
                }

                Timer{
                    id:timerA_ass
                    interval: 700
                    repeat: true
                    triggeredOnStart: false
                    property int thisCounter: 0
                    onTriggered: {
                        refurbishA()
                        thisCounter++
                        if(thisCounter>3) {
                            thisCounter=0
                            stop()
                        }
                    }
                }
            }

            onFocusChanged:{
                if(!focus) return
                refurbishA()
            }
        }

        Item{
            Text {
                y: 20*root.minSize
                color: majorColor
                text: qsTr("B通道过励限制参数")
                anchors.horizontalCenter: parent.horizontalCenter
                verticalAlignment: Text.AlignVCenter
                horizontalAlignment: Text.AlignHCenter
                font.pixelSize: 40*root.minSize
            }

            Rectangle {
                y: 100*root.minSize
                width: 400*root.minSize
                height: 380*root.minSize
                color: "#6692e8bc"
                radius: 10*root.minSize
                anchors.horizontalCenter: parent.horizontalCenter

                ColumnLayout {
                    property real textSize: 30*root.minSize
                    x: 10*root.minSize
                    width: 380*root.minSize
                    anchors.bottom: parent.bottom
                    anchors.top: parent.top

                    EXC_Normal_DoubleTextSytle3{
                        id: text_bNoloadConstantValue
                        width: parent.width
                        Layout.preferredHeight: 40
                        Layout.fillWidth: true
                        funcText: qsTr("空载长期运行值:")
                        valueText: "???"
                        textSize:parent.textSize
                    }
                    EXC_Normal_DoubleTextSytle3{
                        id: text_bLoadConstantValue
                        width: parent.width
                        Layout.preferredHeight: 40
                        Layout.fillWidth: true
                        funcText: qsTr("负载长期运行值:")
                        valueText: "???"
                        textSize:parent.textSize
                    }
                    EXC_Normal_DoubleTextSytle3{
                        id: text_bOverLimit
                        width: parent.width
                        Layout.preferredHeight: 40
                        Layout.fillWidth: true
                        funcText: qsTr("过励限制值:")
                        valueText: "???"
                        textSize:parent.textSize
                    }
                    EXC_Normal_DoubleTextSytle3{
                        id: text_bForceExc
                        width: parent.width
                        Layout.preferredHeight: 40
                        Layout.fillWidth: true
                        funcText: qsTr("强励限制值:")
                        valueText: "???"
                        textSize:parent.textSize
                    }
                    EXC_Normal_DoubleTextSytle3{
                        id: text_bHeatRelSpeed
                        width: parent.width
                        Layout.preferredHeight: 40
                        Layout.fillWidth: true
                        funcText: qsTr("热释放速度:")
                        valueText: "???"
                        textSize:parent.textSize
                    }
                    EXC_Normal_DoubleTextSytle3{
                        id: text_bForceTime
                        width: parent.width
                        Layout.preferredHeight: 40
                        Layout.fillWidth: true
                        funcText: qsTr("强励最长时间:")
                        valueText: "???"
                        textSize:parent.textSize
                    }
                }
            }

            EXC_Normal_BtnUncheckSecret_highStyle {
                id: btn_BSave
                nameFontpixelSize: 25*root.minSize
                y: 410*root.minSize
                width: 140*root.minSize
                height: 50*root.minSize
                anchors.right: parent.right
                anchors.rightMargin: 10
                nameText: qsTr("保存参数")
                onClicked: {
                    writeRegSingalLim(text_bNoloadConstantValue, "B套空载长期运行值")
                    writeRegSingalLim(text_bLoadConstantValue, "B套负载长期运行值")
                    writeRegSingalLim(text_bOverLimit, "B套过励限制值")
                    writeRegSingalLim(text_bForceExc, "B套强励限制值")
                    writeRegSingalLim(text_bHeatRelSpeed, "B套热释放速度")
                    writeRegSingalLim(text_bForceTime, "B套强励最长时间")
                    timerB_ass.start()
                }

                Timer{
                    id:timerB_ass
                    interval: 700
                    repeat: true
                    triggeredOnStart: false
                    property int thisCounter: 0
                    onTriggered: {
                        refurbishB()
                        thisCounter++
                        if(thisCounter>3) {
                            thisCounter=0
                            stop()
                        }
                    }
                }
            }

            onFocusChanged:{
                if(!focus) return
                refurbishB()
            }
        }

        onCurrentIndexChanged: {
            if(sv_this.currentIndex==0) {button.checked=true;button2.checked=false}
            if(sv_this.currentIndex==1) {button2.checked=true;button.checked=false}
        }

    }


    EXC_Normal_BtnSytle {
        id: button
        nameText: qsTr("A通道")
        width: parent.width*0.15
        height: parent.height*0.09
        checked: true
        anchors.left: parent.left
        anchors.leftMargin: parent.width*0.1
        anchors.topMargin: parent.height/32
        anchors.top: sv_this.bottom
        onClicked: {
            if(sv_this.currentIndex==0) checked = true;
            else sv_this.currentIndex = 0
        }
    }

    EXC_Normal_BtnSytle {
        id: button2
        nameText: qsTr("B通道")
        width: parent.width*0.15
        height: parent.height*0.09
        anchors.left: parent.left
        anchors.leftMargin: parent.width*0.43
        anchors.topMargin: parent.height/32
        anchors.top: sv_this.bottom
        onClicked: {
            if(sv_this.currentIndex==1) checked = true;
            else sv_this.currentIndex = 1
        }
    }

    EXC_Normal_BtnUncheckStyle {
        id: button3
        nameText: qsTr("返回")
        nameFontpixelSize: 30*Math.min(root.width/850, root.height/580)
        width: parent.width*0.15
        height: parent.height*0.09
        anchors.left: parent.left
        anchors.leftMargin: parent.width*0.75
        anchors.topMargin: parent.height/32
        anchors.top: sv_this.bottom
        onClicked: {
            sw.currentIndex = 0
        }
    }
}
